/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.client.auth;

import com.nubotech.gwt.oss.client.util.Sha1;

/**
 * 
 * The credential used to authenticate calls to the online storage service
 * @author jonnakkerud
 */
public class Credential {
    public static final String MOCK_EMAIL = "mock";
    public static final String MOCK_PASS = "mock";
    
    private String email;
    private String pass;
    private String account;
    private String secretKey;
    private boolean anonymous = false;

    public Credential() {}
    
    public Credential(String account, String secretKey) {
        this.account = account;
        this.secretKey = secretKey;
    }
    
    /**
     * Email user to help authenticate users.  Only used when storing passwords
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * set the email for the user
     * @param email email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Return the hashed password
     * @return the hashed password
     */
    public String getPass() {
        return pass;
    }

    /**
     * set the password for this account
     * @param pass the password
     */
    public void setPass(String pass) {
        this.pass = pass;
    }

    /**
     * return the account
     * @return the account
     */
    public String getAccount() {
        return account;
    }

    /**
     * set the account
     * @param account the account
     */
    public void setAccount(String account) {
        this.account = account;
    }

    /**
     * return the secret key
     * @return the secret key
     */
    public String getSecretKey() {
        return secretKey;
    }

    /**
     * set the secret key
     * @param key the secret key
     */
    public void setSecretKey(String key) {
        this.secretKey = key;
    }
    
    protected String createKey(String pass) {
        return this.account + this.email + pass;
    }
    
    public String createHashedPass() {
        return Sha1.hex_sha1(this.account + this.pass);
    }

    public boolean isAnonymous() {
        return anonymous;
    }

    /**
     * sets if calls to the service will be anonymous
     * @param anonymous true for anonymous access
     */
    public void setAnonymous(boolean anonymous) {
        this.anonymous = anonymous;
    }

    public String toString() {
        return "account=" + account + " skey="+secretKey;
    }
}
